<?php
/**
 * 原店铺模块数据导入
 *
 * $ID$
 */
class CLI_Module extends CLI
{

    public function main()
    {
        try{
            CLI::output('处理开始');
            //导入图片轮播
            $shopinfos = DB::select('shop_id', 'module_configs')
                ->from('shops')
                ->where('module_configs', '<>', '')
                ->fetch_all();

            foreach($shopinfos as $shopinfo)
            {
                $module_configs = unserialize($shopinfo['module_configs']);
                if(!empty($module_configs['f_images'])&&$module_configs['f_images']['is_enable']==1 && !empty($module_configs['f_images']['data']) && count($module_configs['f_images']['data'])>0){
                     $data = array();
                     $data['module_type'] = 'Flash';
                     $data['module_name'] = '图片轮播';
                     $data['selHeight'] = -1;
                     $data['height'] = 340;
                     $data['img_path'] = array();
                     $data['link'] = array();

                     $i = 0;
                     foreach($module_configs['f_images']['data'] as $v){
                         if ($i == 5)
                             break;
                         if ( ! $v['enable'])
                             continue;

                          $data['link'] = $v['link'];
                          $data['img_path'] = $v['image_md5'];
                          $i=0;
                     }
                     $module_id = Shop_Module::add_module($shopinfo['shop_id'], $data);
                     $data['module_id'] = $module_id;
                     Shop_Module::set_module($shopinfo['shop_id'], $data);

                     DB::update('shop_module_config')->set(array('enabled' => 1))->where('module_id', '=', $module_id)->execute();
                }
            }
            //导入店铺推荐模块
            $modules = DB::select('shop_id', 'module_name', 'item_nums', 'items', 'enabled')
                ->from('shop_modules')
                ->fetch_all();
            foreach($modules as $module)
            {
                $data = array();
                $data['module_name'] = $module['module_name'];
                $data['module_type'] = 'Goods_Push';
                $data['goods_num'] = $module['item_nums'];
                $data['order_type'] = 'sales_desc';

                $data['goods_id'] = explode(',', $module['items']);

                $module_id = Shop_Module::add_module($module['shop_id'], $data);
                $data['module_id'] = $module_id;
                Shop_Module::set_module($module['shop_id'], $data);
                
                DB::update('shop_module_config')->set(array('enabled' => $module['enabled']))->where('module_id', '=', $module_id)->execute();
            }

            CLI::output("\n处理完成");
        }
        catch(Exception $e)
        {
            CLI::output("\n".$e->getMessage());
        }
    }

}